package cn.breeze.basic.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
* @Title:
* @Description: 发送复杂邮件工具类
* @Author: Mr.D
* @Version: 1.0
* @Date:  2022/7/3 0:23
* @Parameters:
* @Return
*/
@Component
public class SendComplexEmails {

    private static SendComplexEmails sendComplexEmails;

    @Autowired
    private JavaMailSender javaMailSender;

    private JavaMailSender getJavaMailSender() {
        return javaMailSender;
    }

    private void setJavaMailSender(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    @PostConstruct
    private void init() {
        sendComplexEmails=new SendComplexEmails();
        sendComplexEmails.setJavaMailSender(javaMailSender);
    }

    public static void sendEmail(String text, String recipient){

    // 2.发送复杂邮件的工具类
        try {
            MimeMessage mimeMessage = sendComplexEmails.getJavaMailSender().createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("857127444@qq.com");
            helper.setSubject("店铺审核通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText(text,true);
            // 5.设置收件人
            helper.setTo(recipient);
            // 6.发送邮件
            sendComplexEmails.getJavaMailSender().send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
